using System;
using Microsoft.Office.Core;
using Send2Sharepoint.Common;
using Send2Sharepoint.Forms;
using Send2Sharepoint.Outlook;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using Exception = System.Exception;

namespace Send2Sharepoint
{
    public partial class Connect
    {
        CommandBarButton _uploadFolderButton;

        CommandBarButton _uploadSelectedButton;

        public void RemoveOutlook()
        {
            _uploadFolderButton.Delete(System.Reflection.Missing.Value);
            _uploadSelectedButton.Delete(System.Reflection.Missing.Value);
            _uploadFolderButton = null;
            _uploadSelectedButton = null;
        }
  
        private void CreateOutlookButtons(_CommandBars bars)
        {
            Icon icon;
            try
            {
                var standardBar = bars["Standard"];
                try
                {
                    _uploadSelectedButton = (CommandBarButton)standardBar.Controls[_rm.GetString("ApplicationName")];
                    _uploadSelectedButton.Delete(true);
                }
                catch
                {
                }

                _uploadSelectedButton = (CommandBarButton)standardBar.Controls.Add(MsoControlType.msoControlButton,
                    System.Reflection.Missing.Value,
                    System.Reflection.Missing.Value,
                    System.Reflection.Missing.Value,
                    System.Reflection.Missing.Value);

                _uploadSelectedButton.Caption = _rm.GetString("ApplicationName");
                _uploadSelectedButton.Style = MsoButtonStyle.msoButtonIconAndCaption;
                _uploadSelectedButton.Tag = "UploadSelectedButton";
                _uploadSelectedButton.OnAction = "!<Send2Sharepoint.Connect>";
                _uploadSelectedButton.Visible = true;
                icon = (Icon)_rm.GetObject("sharepoint");
                var bmp = icon.ToBitmap();
                Clipboard.SetDataObject(bmp, true);
                _uploadSelectedButton.FaceId = 0;
                _uploadSelectedButton.PasteFace();
                _uploadSelectedButton.Click += UploadSelectedButton_Click;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.StackTrace);
                throw;
            }
        }

        public void InitializeOutlook()
        {
            var thisApp = (Microsoft.Office.Interop.Outlook.Application)Globals.ThisApplication;
            try
            {
                var commandBars = thisApp.ActiveExplorer().CommandBars;
                Globals.Root = OutlookFolder.GetRootFolder(thisApp);
                CreateOutlookButtons(commandBars);
            }
            catch (InvalidOperationException ex)
            {
                Logging.WriteException(ex);
                throw new InvalidOperationException(ex.Message);
            }
        }


        static void UploadSelectedButton_Click(CommandBarButton ctrl, ref bool cancelDefault)
        {
            try
            {
                var selectedItems = new ArrayList();
                foreach (var selectedItem in ((Microsoft.Office.Interop.Outlook.Application)Globals.ThisApplication).ActiveExplorer().Selection)
                {
                    selectedItems.Add(selectedItem);
                }
                var uploadSelectedItems = new UploadOutlookSelectedItems(selectedItems)
                                          {
                                              Description = string.Format("{0} :Selected items", "Outlook")
                                          };

                if (_indexDocument != null)
                {
                    if (!_indexDocument.IsDisposed)
                        _indexDocument.BringToFront();
                    else
                    {
                        _indexDocument = new IndexDocument(uploadSelectedItems)
                                             {
                                                 MinimizeBox = false,
                                                 StartPosition = FormStartPosition.CenterScreen
                                             };

                        _indexDocument.Show();
                    }
                }
                else
                {
                    _indexDocument = new IndexDocument(uploadSelectedItems)
                                         {
                                             MinimizeBox = false,
                                             StartPosition = FormStartPosition.CenterScreen
                                         };
                    _indexDocument.Show();
                }
            }
            catch (InvalidOperationException ex)
            {
                Logging.WriteException(ex);
                throw new InvalidOperationException(ex.Message);
            }
            catch
            {
                Logging.WriteToLog(string.Format("{0} :Selected items", "Outlook"));
            }
        }
    }
}
